﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using QLK.Database.BLLs;
using QLK.Database;
using QLK.Web.App_Code;
using Utilities;

namespace QLK.Web
{
    public partial class InvoicesProductDetails : System.Web.UI.Page
    {
        private string _productID;
        ImportInvoiceBLL importBLL = new ImportInvoiceBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            _productID = Request.QueryString[Columns.SupplierID];
            if (!IsPostBack)
            {
                if (_productID != null && _productID != string.Empty)
                {
                    int ID = ConvertHelper.CheckQueryString_Int(_productID);
                    if (ID == -1)
                    {
                        //redirect to error page
                    }
                    else
                    {
                        LoadInvoiceID();
                        LoadInvoiceProductDetails(ID);
                    }
                }
            }
        }
        private void LoadInvoiceID()
        {
            DropInvoiceID.DataSource = importBLL.GetAllImportInvoice();
            DropInvoiceID.DataTextField = "InvoceID";
            DropInvoiceID.DataValueField = "ImportID";
            DropInvoiceID.DataBind();
        }
        private void LoadInvoiceProductDetails(int productID)
        {
            InvoicesProductBLL bll = new InvoicesProductBLL();
            Invoices_Product invoice;
            invoice = bll.GetInvoiceProductByProductID(int.Parse(_productID));
            txtPrice.Text = Request.QueryString[Columns.Price];
            txtQuantity.Text = Request.QueryString[Columns.Quantity];
            txtTotalValues.Text = Request.QueryString[Columns.TotalValues];
            DropInvoiceID.SelectedValue = Request.QueryString[Columns.InvoiceID];
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool res = false;
            InvoicesProductBLL bll = new InvoicesProductBLL();
            Invoices_Product inproduct;
            if (_productID != null && _productID != string.Empty)
            {
                inproduct = bll.GetInvoiceProductByProductID(int.Parse(_productID));
                inproduct.InvoiceID = int.Parse(DropInvoiceID.SelectedValue.ToString());
                inproduct.Price = int.Parse(txtPrice.Text.ToString());
                inproduct.Quantity = int.Parse(txtQuantity.Text.ToString());
                inproduct.TotalValues = decimal.Parse(txtTotalValues.Text.ToString());
                if (bll.UpdateInvoiceProduct(inproduct))
                {
                    lblMsg.Visible = true;
                    lblMsg.Text = "update successfully!";
                    res = true;
                }
            }
            else
            {
                inproduct = new Invoices_Product();
                inproduct.Price = int.Parse(txtPrice.Text.Trim());
                inproduct.Quantity = int.Parse(txtQuantity.Text.Trim());
                inproduct.TotalValues = decimal.Parse(txtTotalValues.Text.Trim());
                inproduct.InvoiceID = int.Parse(DropInvoiceID.SelectedValue.ToString());
                if (bll.InsertInvoiceProduct(inproduct))
                {
                    lblMsg.Visible = true;
                    lblMsg.Text = "insert successfully!";
                    res = true;
                }
            }
            
            if (res)
                Response.Redirect(PageUrls.InvoicesProductManagement);
            else
            {
                // loi
            }
          
        }
    }
}
